package com.lancoo.examview.svga;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.lancoo.examview.R;
import com.opensource.svgaplayer.SVGAClickAreaListener;
import com.opensource.svgaplayer.SVGADrawable;
import com.opensource.svgaplayer.SVGADynamicEntity;
import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.SVGAVideoEntity;

import org.jetbrains.annotations.NotNull;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by pengmi on 2022/4/2 19:52
 * Description:
 */
public class SvgaTestActivity extends AppCompatActivity {

    private SVGAImageView animationView = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {//activity_svga_test.xml
        super.onCreate(savedInstanceState);
        SVGAParser.Companion.shareParser().init(this);
        animationView = new SVGAImageView(this);
        animationView.setOnAnimKeyClickListener(new SVGAClickAreaListener() {
            @Override
            public void onClick(@NotNull String clickKey) {
//                Toast.makeText(SvgaTestActivity.this,clickKey,Toast.LENGTH_SHORT).show();
            }
        });
        animationView.setBackgroundColor(Color.WHITE);
        loadAnimation();
        setContentView(animationView);
    }


    public static void launch(Activity activity){
        Intent mIntent=new Intent(activity,SvgaTestActivity.class);
        activity.startActivity(mIntent);
    }

    private void loadAnimation() {
        SVGAParser.Companion.shareParser().decodeFromAssets("ai_order_1.svga",new SVGAParser.ParseCompletion() {
            @Override
            public void onComplete(@NotNull SVGAVideoEntity videoItem) {
                SVGADynamicEntity dynamicEntity = new SVGADynamicEntity();
                dynamicEntity.setClickArea("img_10");
                SVGADrawable drawable = new SVGADrawable(videoItem, dynamicEntity);
                animationView.setImageDrawable(drawable);
                animationView.startAnimation();
            }
            @Override
            public void onError() {

            }
        },null);
    }
}
